package db;

import beans.B_Cliente;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_Sede;
import conexion.Conexion;
import java.sql.Types;

/**
 *
 * @author JoseHernan
 */
public class DB_Sede {

    public static ArrayList<B_Sede> getListByCliente(int idCliente) {
        ArrayList<B_Sede> list = new ArrayList<B_Sede>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            ps = con.prepareStatement(" SELECT s.id, s.direccion, s.telefono, s.nombre, s.id_cliente, c.razon_social FROM tbl_sede s inner join tbl_cliente c on c.id = s.id_cliente where s.removed = false and s.id_cliente = " + idCliente + " order by c.razon_social ");
            rs = ps.executeQuery();
            B_Sede obj;
            B_Cliente objCliente;
            while (rs.next()) {
                obj = new B_Sede();
                objCliente = new B_Cliente();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setDireccion(rs.getString("direccion"));
                obj.setTelefono(rs.getString("telefono"));
                obj.setNombre(rs.getString("nombre"));
                objCliente.setId(rs.getInt("id_cliente"));
                objCliente.setRazonSocial(rs.getString("razon_social"));
                obj.setObjCliente(objCliente);
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    public static ArrayList<B_Sede> getList(int id) {
        ArrayList<B_Sede> list = new ArrayList<B_Sede>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            if (id == 0) {
                ps = con.prepareStatement(" SELECT s.id, s.direccion, s.telefono, s.nombre, s.id_cliente, c.razon_social FROM tbl_sede s inner join tbl_cliente c on c.id = s.id_cliente where s.removed = false order by c.razon_social ");
            } else {
                ps = con.prepareStatement(" SELECT s.id, s.direccion, s.telefono, s.nombre, s.id_cliente, c.razon_social FROM tbl_sede s inner join tbl_cliente c on c.id = s.id_cliente where c.id = " + id);
            }
            rs = ps.executeQuery();
            B_Sede obj;
            B_Cliente objCliente;
            while (rs.next()) {
                obj = new B_Sede();
                objCliente = new B_Cliente();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setDireccion(rs.getString("direccion"));
                obj.setTelefono(rs.getString("telefono"));
                obj.setNombre(rs.getString("nombre"));
                objCliente.setId(rs.getInt("id_cliente"));
                objCliente.setRazonSocial(rs.getString("razon_social"));
                obj.setObjCliente(objCliente);
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_Sede obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_sede(?,?,?,?,?,?,?,?)}");

            cs.registerOutParameter("p_message", Types.VARCHAR);
            cs.setString("p_action", action);
            cs.setInt("p_id", obj.getId());
            cs.setString("p_direccion", obj.getDireccion());
            cs.setString("p_telefono", obj.getTelefono());
            cs.setString("p_nombre", obj.getNombre());
            cs.setInt("p_id_cliente", obj.getObjCliente().getId());
            cs.setString("p_user", obj.getUser());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }
}
